package com.xiaoyy.app.extend.gateway.config;

import com.xiaoyy.core.common.enums.ErrorEnum;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.AppResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ErrorUtil {
    public static AppResponse errorResponse(Exception ex){
        if(ex instanceof AppException) {
            AppException appException = (AppException) ex;
            if (appException.getCode()==null){
                return  AppResponse.error(appException.getMsg(),appException.getData());
            }
            return AppResponse.error(appException.getCode(),appException.getMsg(),appException.getData());
        }else if (ex instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException m = (MissingServletRequestParameterException) ex;
            return AppResponse.errorMsg("缺少参数:" + m.getParameterName() + "不存在");
        } else if (ex instanceof MissingServletRequestPartException){
            MissingServletRequestPartException m = (MissingServletRequestPartException) ex;
            return AppResponse.errorMsg("缺少参数:" + m.getRequestPartName() + "不存在");
        }else if (ex instanceof MethodArgumentNotValidException) {
            BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
            String errorMesssage = null;
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                errorMesssage = fieldError.getDefaultMessage();
            }
            if (StringUtils.isBlank(errorMesssage) || errorMesssage.equals("不能为空")){
                errorMesssage = "参数校验不通过";
            }
            return AppResponse.errorMsg(errorMesssage);
        } else if (ex instanceof HttpMessageNotReadableException) {
            return AppResponse.errorMsg("参数格式转换出错");
        } else if (ex instanceof SQLException){
            return AppResponse.errorMsg("数据库处理异常："+ex.getMessage());
        }else if (ex instanceof RuntimeException) {
            //if (containsHanScript(ex.getMessage())) {
            //    return AppResponse.errorMsg(getFirstHanGroup(ex.getMessage()));
            //}
            return AppResponse.error(-1,"系统异常："+ex.getMessage());
        }
        return AppResponse.error(ErrorEnum.SYSTEM_ERROR.getCode(), ErrorEnum.SYSTEM_ERROR.getMessage(),null);
    }
    /**
     * 是否包含汉字
     */
    public static boolean containsHanScript(String s) {
        if(s == null || s.length() == 0){
            return false;
        }

        return s.codePoints().anyMatch(
                codePoint ->
                        Character.UnicodeScript.of(codePoint) == Character.UnicodeScript.HAN);
    }

    /**
     * 提取汉字(只提取第一组汉字)
     */
    public static String getFirstHanGroup(String s) {
        if(s == null || s.length() == 0){
            return "";
        }

        String regex="([\u4e00-\u9fa5]+)";
        Matcher matcher = Pattern.compile(regex).matcher(s);
        if(matcher.find()){
            return matcher.group(0);
        }
        return "";
    }

}
